package com.woniu.tms.attendance.service.impl;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.tms.attendance.service.AttendanceService;
import com.woniu.tms.attendance.util.DateUtil;
import com.woniu.tms.entity.*;
import com.woniu.tms.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @Author: peng
 */
@Service
@Transactional
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    CheckinfoMapper checkinfoMapper;
    @Autowired
    ClassesMapper classesMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    SickformMapper sickformMapper;

    private static ObjectMapper MAPPER = new ObjectMapper();

    @Override
    public List<Checkinfo> listSelf(CheckinfoExample example) {
        return checkinfoMapper.selectByExample(example);
    }

    /**
     * 展示讲师所属班级学生的考勤信息
     * 展示班主任所属班级学生的考勤信息
     * 教学主管查询所有学员的考勤信息
     *
     * @param uid
     * @param examplesMap
     * @param pageNum
     * @param pageSize
     * @return
     * @throws IOException
     */
    @Override
    public Map<String, Object> listSelfClassByTid(Integer uid, String examplesMap, Integer pageNum, Integer pageSize) throws IOException {
        Map<String, Object> map = new HashMap<>();
        /**
         * 班级查询用例
         */
        ClassesExample classesExample = new ClassesExample();
        ClassesExample.Criteria classesExampleCriteria = classesExample.createCriteria();
        /**
         * 用户查询用例
         */
        UserExample userExample = new UserExample();
        UserExample.Criteria userExampleCriteria = userExample.createCriteria();
        /**
         * 考勤信息查询用例
         */
        CheckinfoExample checkinfoExample = new CheckinfoExample();
        CheckinfoExample.Criteria checkinfoExampleCriteria = checkinfoExample.createCriteria();
        /**
         * 根据uid判断，角色为讲师还是班主任
         */
        UserRoleExample userRoleExample = new UserRoleExample();
        UserRoleExample.Criteria userRoleExampleCriteria = userRoleExample.createCriteria();
        userRoleExampleCriteria.andUidEqualTo(uid);
        List<UserRole> userRoleList = userRoleMapper.selectByExample(userRoleExample);
        Integer rid = userRoleList.get(0).getRid();


        /**
         * 将前台传递的json字符传转换为map集合
         */
        if (examplesMap != null && !examplesMap.equals("")) {
            JavaType jvt = MAPPER.getTypeFactory().constructParametricType(HashMap.class, String.class, String.class);
            Map<String, String> urMap = MAPPER.readValue(examplesMap, jvt);

            if (urMap != null && urMap.size() != 0) {

                /**
                 * 起始日期minDate
                 */
                if (urMap.get("minDate") != null && !urMap.get("minDate").equals("")) {
                    checkinfoExampleCriteria.andTimesGreaterThanOrEqualTo(new Date(urMap.get("minDate")));
                }
                /**
                 * 为结束日期maxDate
                 */
                if (urMap.get("maxDate") != null && !urMap.get("maxDate").equals("")) {
                    checkinfoExampleCriteria.andTimesLessThanOrEqualTo(new Date(urMap.get("maxDate")));
                }
                /**
                 * 为考勤状态cfstatus
                 */
                if (urMap.get("cfstatus") != null && !urMap.get("cfstatus").equals("")) {
                    checkinfoExampleCriteria.andCfstatusEqualTo(urMap.get("cfstatus"));
                }

                /**
                 * 为学员姓名nameLike,使用用户用例
                 */
                if (urMap.get("nameLike") != null && !urMap.get("nameLike").equals("")) {
                    userExampleCriteria.andUnameLike("%" + urMap.get("nameLike") + "%");
                }

                /**
                 * 为班级用例class，使用班级用例
                 */
                if (urMap.get("classes") != null && !urMap.get("classes").equals("")) {
                    classesExampleCriteria.andCidEqualTo(Integer.parseInt(urMap.get("classes")));
                    map.put("classesid", urMap.get("classes"));
                }
            }
        }

        /**
         * 根据班主任或是讲师id查询插入不同的条件
         * 若为教学主管，则查询全部班级学员的考勤信息
         */
        if (rid == 2) {
            /**
             * 2为班主任
             *
             */
            classesExampleCriteria.andClassmanageridEqualTo(uid);
        } else if (rid == 3) {
            /**
             * 3为讲师
             */
            classesExampleCriteria.andTeacheridEqualTo(uid);
        }

        /**
         * 从班级列表中出发，根据讲师id或是班主任查询到的班级列表
         * 若为教学主管，查询全部班级到全部班级的信息
         */
        List<Classes> classesList = classesMapper.selectByExample(classesExample);

        List<Integer> cids = new ArrayList<>();
        for (Classes classes : classesList) {
            cids.add(classes.getCid());
        }

        /**
         * 根据cid，查询该班级的全部学生
         */
        List<User> studentList = new ArrayList<>();
        if (cids.size() > 0) {
            userExampleCriteria.andClassidIn(cids);
            studentList = userMapper.selectByExample(userExample);
        }
        /**
         * 学生id集合
         */
        List<Integer> studentids = new ArrayList<>();
        for (User user : studentList) {
            studentids.add(user.getUid());
        }
        /**
         * 根据学生id查询其全部考勤信息
         */
        PageHelper.startPage(pageNum, pageSize);
        List<Checkinfo> checkinfoList = new ArrayList<>();

        if (studentids.size() > 0) {
            checkinfoExampleCriteria.andStudentidIn(studentids);
            checkinfoList = checkinfoMapper.selectByExample(checkinfoExample);
            PageInfo<Checkinfo> pageInfo = new PageInfo<>(checkinfoList);
            map.put("checkinfoList", checkinfoList);
            map.put("pageInfo", pageInfo);
        }


        return map;
    }

    /**
     * 根据讲师id或班主任id查询其对应的班级
     *
     * @param uid
     * @return
     */
    @Override
    public List<Classes> listClassesByTidOrCmid(Integer uid) {
        /**
         * 根据uid判断，角色为讲师还是班主任
         */
        UserRoleExample userRoleExample = new UserRoleExample();
        UserRoleExample.Criteria userRoleExampleCriteria = userRoleExample.createCriteria();
        userRoleExampleCriteria.andUidEqualTo(uid);
        List<UserRole> userRoleList = userRoleMapper.selectByExample(userRoleExample);
        Integer rid = userRoleList.get(0).getRid();

        ClassesExample classesExample = new ClassesExample();
        ClassesExample.Criteria classesExampleCriteria = classesExample.createCriteria();

        if (rid == 2) {
            /**
             * 2为班主任
             *
             */
            classesExampleCriteria.andClassmanageridEqualTo(uid);
        } else if (rid == 3) {
            /**
             * 3为讲师
             */
            classesExampleCriteria.andTeacheridEqualTo(uid);
        }

        return classesMapper.selectByExample(classesExample);
    }

    /**
     * 根据讲师id或班主任id查询状态为待审批需要其审批的请假信息
     *
     * @param confirmid
     * @return
     */
    @Override
    public List<Sickform> listSickInfo(Integer confirmid) {
        SickformExample example = new SickformExample();
        SickformExample.Criteria criteria = example.createCriteria();
        criteria.andConfirmidEqualTo(confirmid);
        criteria.andStatusEqualTo("待审批");
        return sickformMapper.selectByExample(example);
    }

    /**
     * 根据主键查询考勤信息
     *
     * @param cfid
     * @return
     */
    @Override
    public Checkinfo selectByCfid(Integer cfid) {
        return checkinfoMapper.selectByPrimaryKey(cfid);
    }

    /**
     * 班主任修改考勤信息
     *
     * @param checkinfo
     */
    @Override
    public void confirmUpdate(Checkinfo checkinfo) {
        checkinfoMapper.updateByPrimaryKeySelective(checkinfo);
    }

    /**
     * 班主任查询其对应班级的全部学员信息
     *
     * @param uid         班主任的uid
     * @param examplesMap 查询条件uid
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Map<String, Object> listStudent(Integer uid, String examplesMap, Integer pageNum, Integer pageSize) throws IOException {
        Map<String, Object> map = new HashMap<>();
        /**
         * 班级查询用例
         */
        ClassesExample classesExample = new ClassesExample();
        ClassesExample.Criteria classesExampleCriteria = classesExample.createCriteria();
        /**
         * 用户查询用例
         */
        UserExample userExample = new UserExample();
        UserExample.Criteria userExampleCriteria = userExample.createCriteria();

        /**
         * 将前台传递的json字符传转换为map集合
         */
        if (examplesMap != null && !examplesMap.equals("")) {
            JavaType jvt = MAPPER.getTypeFactory().constructParametricType(HashMap.class, String.class, String.class);
            Map<String, String> urMap = MAPPER.readValue(examplesMap, jvt);

            if (urMap != null && urMap.size() != 0) {

                /**
                 * 为学员姓名nameLike,使用用户用例
                 */
                if (urMap.get("nameLike") != null && !urMap.get("nameLike").equals("")) {
                    userExampleCriteria.andUnameLike("%" + urMap.get("nameLike") + "%");
                }

                /**
                 * 为班级用例class，使用班级用例
                 */
                if (urMap.get("classes") != null && !urMap.get("classes").equals("")) {
                    classesExampleCriteria.andCidEqualTo(Integer.parseInt(urMap.get("classes")));
                    map.put("classesid", urMap.get("classes"));
                }
            }
        }

        /**
         * 从班级列表中出发，根据班主任查询到的班级列表
         */

        classesExampleCriteria.andClassmanageridEqualTo(uid);
        List<Classes> classesList = classesMapper.selectByExample(classesExample);
        List<Integer> cids = new ArrayList<>();
        for (Classes classes : classesList) {
            cids.add(classes.getCid());
        }

        PageHelper.startPage(pageNum, pageSize);

        /**
         * 根据cid，查询该班级的全部学生
         */
        List<User> studentList = new ArrayList<>();

        if (cids.size() > 0) {
            userExampleCriteria.andClassidIn(cids);
            studentList = userMapper.selectByExample(userExample);
            PageInfo<User> pageInfo = new PageInfo<>(studentList);
            map.put("pageInfo", pageInfo);
            map.put("studentList", studentList);
        }
        return map;
    }

    /**
     * 批量增加学员考勤信息
     *
     * @param paramMap
     */
    @Override
    public void addSomeCheckinfo(Map<String, Object> paramMap) {
        checkinfoMapper.addSome(paramMap);
    }

    /**
     * 批量删除学员考勤信息
     *
     * @param ids
     */
    @Override
    public void delSome(Integer[] ids) {
        CheckinfoExample example = new CheckinfoExample();
        CheckinfoExample.Criteria criteria = example.createCriteria();
        criteria.andCfidIn(Arrays.asList(ids));
        checkinfoMapper.deleteByExample(example);
    }

    /**
     * 查询所有学员的出勤率
     *
     * @return
     */
    @Override
    public Map<String, Object> listAttendance(Integer pageNum, Integer pageSize, String exampleMap) throws IOException {

        Map<String, Object> map = new HashMap<>();
        /**
         * 角色查询用例：学员角色id为1
         */
        UserRoleExample userRoleExample = new UserRoleExample();
        UserRoleExample.Criteria criteria1 = userRoleExample.createCriteria();
        criteria1.andRidEqualTo(1);
        List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample);
        /**
         * 获取全部角色为学生的uid
         */
        List<Integer> uids = new ArrayList<>();
        for (UserRole userRole : userRoles) {
            uids.add(userRole.getUid());
        }

        PageHelper.startPage(pageNum, pageSize);
        /**
         * 用户查询example
         */
        UserExample userexample = new UserExample();
        UserExample.Criteria userExampleCriteria = userexample.createCriteria();
        /**
         * 插入查询条件
         */
        if (exampleMap != null && !exampleMap.equals("")) {
            JavaType jvt = MAPPER.getTypeFactory().constructParametricType(HashMap.class, String.class, String.class);
            Map<String, String> urMap = MAPPER.readValue(exampleMap, jvt);
            if (urMap != null && urMap.size() != 0) {
                /**
                 * 为学员姓名nameLike,使用用户用例
                 */
                if (urMap.get("nameLike") != null && !urMap.get("nameLike").equals("")) {
                    userExampleCriteria.andUnameLike("%" + urMap.get("nameLike") + "%");
                }
            }
        }
        /**
         * 若uids的size不为0
         */
        List<User> userList = new ArrayList<>();
        if (uids.size() > 0) {
            userExampleCriteria.andUidIn(uids);
            userList = userMapper.selectByExample(userexample);
            PageInfo<User> pageInfo = new PageInfo<>(userList);
            map.put("pageInfo", pageInfo);
        }

        for (int i = 0; i < userList.size(); i++) {


            /**
             * 学生总考勤次数
             */
            Integer count = selectCount(userList.get(i).getUid(), null).intValue();


            /**
             * 学生出勤次数
             */
            Double normal = selectCount(userList.get(i).getUid(), "正常");

            /**
             * 学生迟到次数
             */
            Double late = selectCount(userList.get(i).getUid(), "迟到");

            /**
             * 学生请假次数
             */
            Double vacate = selectCount(userList.get(i).getUid(), "请假");

            DecimalFormat df = new DecimalFormat("0.00");

            if (!count.equals(0)) {

                String temp1 = df.format(normal / count * 100);
                userList.get(i).setNormal(Double.parseDouble(temp1));

                String temp2 = df.format(late / count * 100);
                userList.get(i).setLate(Double.parseDouble(temp2));

                String temp3 = df.format(vacate / count * 100);
                userList.get(i).setVacate(Double.parseDouble(temp3));
            }
        }
        map.put("userList", userList);

        return map;
    }

    /**
     * 根据学生id及考勤状态，该学生不同考勤状态下的出勤率以及迟到率、请假率
     *
     * @param studentid
     * @param cfstatus
     * @return
     */
    @Override
    public Double selectCount(Integer studentid, String cfstatus) {
        CheckinfoExample example = new CheckinfoExample();
        CheckinfoExample.Criteria criteria = example.createCriteria();
        if (cfstatus != null && !cfstatus.equals("")) {
            criteria.andCfstatusEqualTo(cfstatus);
        }
        criteria.andStudentidEqualTo(studentid);
        Long count = checkinfoMapper.countByExample(example);
        return count.doubleValue();
    }

    /**
     * 教学主管根据班主任id查询其对应班级的考勤率，初次显示全部班级的考勤率(根据传入时间范围的不同)
     *
     * @param pageNum
     * @param pageSzie
     * @param examplesMap
     * @return
     */
    @Override
    public Map<String, Object> listAttendanceByClassManagerid(Integer pageNum, Integer pageSzie, String examplesMap) throws IOException {
        /**
         * 需要返回的map
         */
        Map<String, Object> map = new HashMap<>();
        /**
         * 用户example
         */
        UserExample userExample = new UserExample();
        UserExample.Criteria userExampleCriteria = userExample.createCriteria();
        /**
         * 班级example
         */
        ClassesExample classesExample = new ClassesExample();
        ClassesExample.Criteria classesExampleCriteria = classesExample.createCriteria();
        /**
         * 考勤信息example
         */
        CheckinfoExample checkinfoExample = new CheckinfoExample();
        CheckinfoExample.Criteria checkinfoExampleCriteria = checkinfoExample.createCriteria();


        /**
         * 将前台传递的json字符传转换为map集合
         */
        if (examplesMap != null && !examplesMap.equals("")) {

            JavaType jvt = MAPPER.getTypeFactory().constructParametricType(HashMap.class, String.class, String.class);
            Map<String, String> urMap = MAPPER.readValue(examplesMap, jvt);

            /**
             * 初次运行时查询全部考勤信息统计出的考勤率
             */
            if (urMap != null && urMap.size() != 0) {

                /**
                 * 年份
                 */
                if (urMap.get("year") != null && !urMap.get("year").equals("")) {
                    int year = Integer.parseInt(urMap.get("year"));
                    int nowYear = new Date().getYear() + 1900;
                    if (year > nowYear) {
                        throw new RuntimeException("选择年份大于当前年份");
                    } else if (year == nowYear) {
                        /**
                         * 截至查询日期的全部考勤日期
                         */
                        Date beginDate = new Date(year + "/01/01");
                        Date endDate = new Date();

                        checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);

                    } else if (year < nowYear) {
                        Date beginDate = new Date(year + "/01/01");
                        Date endDate = new Date(year + "/12/31");
                        checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                    }
                }

                /**
                 * 季度
                 */
                if (urMap.get("quarter") != null && !urMap.get("quarter").equals("")) {
                    checkinfoExampleCriteria.getAllCriteria().clear();
                    /**
                     * 获取条件季度
                     */
                    int quarter = Integer.parseInt(urMap.get("quarter"));

                    int year = Integer.parseInt(urMap.get("year"));
                    /**
                     * 获取当前日期所处季度
                     */
                    Date now = new Date();
                    int nowYear = now.getYear() + 1900;
                    int nowMonth = now.getMonth() + 1;
                    int nowQuarter = 0;

                    if (nowMonth >= 1 && nowMonth <= 3) {
                        nowQuarter = 1;
                    } else if (nowMonth >= 4 && nowMonth <= 6) {
                        nowQuarter = 2;
                    } else if (nowMonth >= 7 && nowMonth <= 9) {
                        nowQuarter = 3;
                    } else if (nowMonth >= 10 && nowMonth <= 12) {
                        nowQuarter = 4;
                    }

                    if (year > nowYear) {
                        throw new RuntimeException("条件年份不能大于于当前年份");
                    } else if (year == nowYear) {

                        if (quarter < nowQuarter) {
                            if (quarter == 1) {
                                Date beginDate = new Date(year + "/01/01");
                                Date endDate = new Date(year + "/03/31");
                                checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                            } else if (quarter == 2) {
                                Date beginDate = new Date(year + "/04/01");
                                Date endDate = new Date(year + "/06/30");
                                checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                            } else if (quarter == 3) {
                                Date beginDate = new Date(year + "/07/01");
                                Date endDate = new Date(year + "/09/30");
                                checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                            } else if (quarter == 4) {
                                Date beginDate = new Date(year + "/10/01");
                                Date endDate = new Date();
                                checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                            }
                        } else if (quarter == nowQuarter) {
                            Date endDate = new Date();
                            if (nowQuarter == 1) {
                                Date beginDate = new Date(year + "/01/01");
                                checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                            } else if (nowQuarter == 2) {
                                Date beginDate = new Date(year + "/04/01");
                                checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                            } else if (nowQuarter == 3) {
                                Date beginDate = new Date(year + "/07/01");
                                checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                            } else if (nowQuarter == 4) {
                                Date beginDate = new Date(year + "/10/01");
                                checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                            }
                        } else if (quarter > nowQuarter) {
                            throw new RuntimeException("选择季度超出当前日期所属季度");
                        }
                    } else if (year < nowYear) {
                        if (quarter == 1) {
                            Date beginDate = new Date(year + "/01/01");
                            Date endDate = new Date(year + "/03/31");
                            checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                        } else if (quarter == 2) {
                            Date beginDate = new Date(year + "/04/01");
                            Date endDate = new Date(year + "/06/30");
                            checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                        } else if (quarter == 3) {
                            Date beginDate = new Date(year + "/07/01");
                            Date endDate = new Date(year + "/09/30");
                            checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                        } else if (quarter == 4) {
                            Date beginDate = new Date(year + "/10/01");
                            Date endDate = new Date(year + "/12/31");
                            checkinfoExampleCriteria.andTimesBetween(beginDate, endDate);
                        }
                    }
                }
                /**
                 * 月
                 */
                if (urMap.get("month") != null && !urMap.get("month").equals("")) {
                    checkinfoExampleCriteria.getAllCriteria().clear();
                    int year = Integer.parseInt(urMap.get("year"));
                    int month = Integer.parseInt(urMap.get("month"));
                    String monthFirtDay = year + "/" + month + "/1";
                    Date beginDay = new Date(monthFirtDay);
                    Date endDay = DateUtil.getEndDayOfAnyMonth(year, month);
                    checkinfoExampleCriteria.andTimesBetween(beginDay, endDay);
                }
                /**
                 * 周
                 */
                if (urMap.get("week") != null && !urMap.get("week").equals("")) {
                    checkinfoExampleCriteria.getAllCriteria().clear();
                    int year = Integer.parseInt(urMap.get("year"));
                    if (urMap.get("month") == null || urMap.get("month").equals("")) {
                        throw new RuntimeException("月份不能为空");
                    }
                    int month = Integer.parseInt(urMap.get("month"));

                    int week = Integer.parseInt(urMap.get("week"));

                    Date now = new Date();
                    int nowMonth = now.getMonth() + 1;

                    if (month > nowMonth) {
                        throw new RuntimeException("输入的月份大于当前月");
                    } else if (month == nowMonth) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(now);
                        Integer nowWeek = calendar.get(Calendar.WEEK_OF_MONTH);
                        if (week > nowWeek) {
                            throw new RuntimeException("输入的周大于当前周");
                        } else if (week == nowWeek) {
                            /**
                             * 获得输入周的最后一天
                             */
                            Date endDay = DateUtil.getEndDayOfAnyWeek(new Date(year + "/" + month + "/01"), week);
                            /**
                             * 获得输入周的第一天
                             */
                            Calendar instance = Calendar.getInstance();
                            instance.setTime(endDay);
                            instance.set(Calendar.DATE, instance.get(Calendar.DATE) - 6);
                            Date beginDay = instance.getTime();
                            /**
                             * 将结束日期设置为当前日期
                             */
                            endDay = new Date();

                            checkinfoExampleCriteria.andTimesBetween(beginDay, endDay);
                        } else if (week < nowWeek) {
                            /**
                             * 获得输入周的最后一天
                             */
                            Date endDay = DateUtil.getEndDayOfAnyWeek(new Date(year + "/" + month + "/01"), week);
                            /**
                             * 获得输入周的第一天
                             */
                            Calendar instance = Calendar.getInstance();
                            instance.setTime(endDay);
                            instance.set(Calendar.DATE, instance.get(Calendar.DATE) - 6);
                            Date beginDay = instance.getTime();

                            checkinfoExampleCriteria.andTimesBetween(beginDay, endDay);
                        }

                    } else if (month < nowMonth) {
                        /**
                         * 获得输入周的最后一天
                         */
                        Date endDay = DateUtil.getEndDayOfAnyWeek(new Date(year + "/" + month + "/01"), week);
                        /**
                         * 获得输入周的第一天
                         */
                        Calendar instance = Calendar.getInstance();
                        instance.setTime(endDay);
                        instance.set(Calendar.DATE, instance.get(Calendar.DATE) - 6);
                        Date beginDay = instance.getTime();

                        checkinfoExampleCriteria.andTimesBetween(beginDay, endDay);
                    }
                }
                /**
                 * 日
                 */
                if (urMap.get("day") != null && !urMap.get("day").equals("")) {
                    checkinfoExampleCriteria.getAllCriteria().clear();
                    checkinfoExampleCriteria.andTimesEqualTo(new Date(urMap.get("day")));
                }

                /**
                 * 班主任id
                 */
                if (urMap.get("classmanager") != null && !urMap.get("classmanager").equals("")) {
                    classesExampleCriteria.andClassmanageridEqualTo(Integer.parseInt(urMap.get("classmanager")));
                    User classmanager = new User();
                    classmanager = selectOne(Integer.parseInt(urMap.get("classmanager")));
                    map.put("classmanager", classmanager);
                }
            }

        }
        /**
         * 根据班主任id查询班级,班级集合
         */
        List<Classes> classesList = classesMapper.selectByExample(classesExample);
        /**
         * 形成班级id集合
         */
        List<Integer> cids = new ArrayList<>();
        for (Classes c : classesList) {
            cids.add(c.getCid());
        }

        List<Integer> studentids = new ArrayList<>();

        if (!cids.isEmpty()) {
            userExampleCriteria.andClassidIn(cids);
            List<User> userList = userMapper.selectByExample(userExample);
            for (User user : userList) {
                studentids.add(user.getUid());
            }
        }
        /**
         * 根据学生id查出所有的考勤信息
         */
        List<Checkinfo> checkinfoList = new ArrayList<>();
        if (!studentids.isEmpty()) {
            checkinfoExampleCriteria.andStudentidIn(studentids);
            checkinfoList = checkinfoMapper.selectByExample(checkinfoExample);
        }
        /**
         * 存储考勤率
         */
        Map<String, Object> resultMap = new HashMap<>();
        Integer normal = 0;
        Integer late = 0;
        Integer vacate = 0;
        for (Checkinfo checkinfo : checkinfoList) {
            if (checkinfo.getCfstatus().equals("正常")) {
                normal++;
            } else if (checkinfo.getCfstatus().equals("迟到")) {
                late++;
            } else if (checkinfo.getCfstatus().equals("请假")) {
                vacate++;
            }
        }
        Integer count = checkinfoList.size();
        DecimalFormat df = new DecimalFormat("0.00");
        if (!count.equals(0)) {
            String temp1 = df.format(normal * 1.0 / count * 100);
            String temp2 = df.format(late * 1.0 / count * 100);
            String temp3 = df.format(vacate * 1.0 / count * 100);
            resultMap.put("normal", temp1);
            resultMap.put("late", temp2);
            resultMap.put("vacate", temp3);
            map.put("resultMap", resultMap);

        } else {
            throw new RuntimeException("当前查询条件下无考勤信息");
        }

        return map;
    }

    /**
     * 查询所有的班主任
     *
     * @return
     */
    @Override
    public List<User> getClassManager() {
        UserExample userExample = new UserExample();
        UserExample.Criteria userExampleCriteria = userExample.createCriteria();

        UserRoleExample userRoleExample = new UserRoleExample();
        UserRoleExample.Criteria userRoleExampleCriteria = userRoleExample.createCriteria();
        userRoleExampleCriteria.andRidEqualTo(2);
        List<UserRole> userRoleList = userRoleMapper.selectByExample(userRoleExample);
        List<Integer> uids = new ArrayList<>();
        for (UserRole ur : userRoleList) {
            uids.add(ur.getUid());
        }

        List<User> userList = new ArrayList<>();
        if (!uids.isEmpty()) {
            userExampleCriteria.andUidIn(uids);
            userList = userMapper.selectByExample(userExample);
        }
        return userList;
    }

    @Override
    public User selectOne(Integer uid) {
        return userMapper.selectByPrimaryKey(uid);
    }

    @Override
    public List<Checkinfo> listSelfClassByCmid(User classmanager, CheckinfoExample example) {
        return null;
    }

    @Override
    public List<Checkinfo> listAll(String className, Integer classmanagerid, CheckinfoExample example) {
        return null;
    }

    @Override
    public List<Checkinfo> lisAll(CheckinfoExample example) {
        return null;
    }
}
